﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;

namespace NMVN.Common
{
    public class AutoCompleteComboBoxEditingControl : AutoCompleteComboBox, IDataGridViewEditingControl
    {
        private DataGridView _dataGridView;
        private int _rowIndex;
        private bool _hasValueChanged = false;
        private AutoCompleteComboBoxCell currentCell = null;        

        public AutoCompleteComboBoxEditingControl()
        {
            
        }

        public AutoCompleteComboBoxCell OwnerCell
        {
            get
            {
                return currentCell;
            }
            set
            {
                if (value.Value != null)
                {
                    //Clear currentCell so DoSelectedValueChanged doesn't cause an endless loop
                    currentCell = null;
                    //Set SelectedValue                 
                    if (value.Value1 != null)
                    {                                  
                        base.SelectedValue = value.Value1;                       
                    }
                    //Show that the value hasn't changed yet
                    _hasValueChanged = false;                    
                    //Finally remember the new Owner Cell
                    currentCell = value;
                }
            }
        }


        protected override void OnSelectedValueChanged(EventArgs e)
        {
            if (base.SelectedValue != null && currentCell != null)
            {
                try
                {
                    int isnumber = Convert.ToInt32(base.SelectedValue.ToString());
                    _hasValueChanged = true;
                    currentCell.Value1 = base.SelectedValue;
                    currentCell.DisplayValue = base.Text;
                    currentCell.Value = base.SelectedValue;
                    EditingControlDataGridView.NotifyCurrentCellDirty(true);
                }
                catch (FormatException)
                {
                    currentCell = null;
                }
            }            
            base.OnSelectedValueChanged(e);            
        }      
        
     

        ////Implements IDataGridViewEditingControl      
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
        }

        public DataGridView EditingControlDataGridView
        {
            get
            {
                return _dataGridView;
            }
            set
            {
                _dataGridView = value;
            }
        }

        public object EditingControlFormattedValue
        {
            get
            {
                return base.SelectedValue;
            }
            set
            {
                base.SelectedValue = value;
            }
        }

        public int EditingControlRowIndex
        {
            get
            {
                return _rowIndex;
            }
            set
            {
                _rowIndex = value;
            }
        }

        public bool EditingControlValueChanged
        {
            get
            {
                return _hasValueChanged;
            }
            set
            {
                _hasValueChanged = value;
            }
        }

        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch (keyData & Keys.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.PageDown:
                case Keys.PageUp:
                case Keys.Enter:
                case Keys.Escape:
                case Keys.Delete:
                    return true;
                default:
                    return false;
            }
        }

        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return base.SelectedValue;
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
            
        }

        public bool RepositionEditingControlOnValueChange
        {
            get
            {                
                return false ;
            }
        }



    }
}
